package com.dp.appi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by xiaoji on 2016/1/13.
 */
public class DateUtil {
    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }
    public static String getNowDateStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(new Date());
    }

    public static String getNowDate(String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }

    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return sdf.format(new Date());
    }

    /**
     * 日期转换成字符串
     * @param d
     * @param format
     * @return str
     */
    public static String dateToStr(Date d, String format){
        if(StringUtil.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);//设置日期格式
        return sdf.format(d);
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str,String format) {

        if(StringUtil.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 格式化日期字符串
     * 比如“2016-4”转换为“2016-04”
     * @param str
     * @param format
     * @return
     */
    public  static String formatDateStr(String str,String format){
        return dateToStr(StrToDate(str,format),format);
    }

    /**
     * 两字符串日期比较大小
     * @param date1
     * @param date2
     * @param format
     * @return
     */
    public  static int compareStrTo(String date1,String date2,String format){
        Date d1 = StrToDate(date1,format);
        Date d2 = StrToDate(date2,format);
        return d1.compareTo(d2);
    }

    /**
     * 计算未来日期
     * @param vaild_time
     * @param time_unit
     * @param start_time
     * @return
     */
    public static String calculateFutureDate(String vaild_time, String time_unit, String start_time){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try{
            Date d = sdf.parse(start_time);
            cal.setTime(d);

            if("1".equals(time_unit)){//日
                cal.add(Calendar.DATE, Integer.valueOf(vaild_time));
            }else if("2".equals(time_unit)){//月
                cal.add(Calendar.MONTH, Integer.valueOf(vaild_time));
            }else{//年
                cal.add(Calendar.YEAR, Integer.valueOf(vaild_time));
            }

            return sdf.format(cal.getTime());
        }catch (Exception e){
            return "";
        }
    }

    public static void main(String[] s) throws ParseException {
//        System.out.println(compareStrTo("2016-4","2016-05","yyyy-MM"));
        System.out.println(getNowDate("yyyy"));
    }
}
